import type { NextApiRequest, NextApiResponse } from 'next';
import { BeError, BeSuccess } from '@/utils/response';
import { syncPostNewMR } from '@/services/gitlab/index';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method?.toLocaleLowerCase() !== 'post') {
    res.status(200).json(BeError('不支持请求类型'));
    res.end();
    return;
  }
  const { body, cookies } = req;
  try {
    const result = await syncPostNewMR({ ...body, token: cookies['user-token'] });
    res.status(200).json(BeSuccess(result));
  } catch (error: any) {
    if (error?.response?.status === 409) {
      res.status(200).json(BeError(error, 409));
    }
    res.status(500).json(BeError(error));
  }
}
